package com.example.myapplication2.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapplication2.app.R;
import com.example.myapplication2.app.entity.Fruit;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Dean.Hwang
 * Date: 2016/2/15
 * Time: 10:13
 * Description://TODO
 */
public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourceId;

    public FruitAdapter(Context context, int resourceId, List<Fruit> objs) {
        super(context, resourceId, objs);
        this.resourceId = resourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = getItem(position); // 获取当前项的Fruit实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.fruitImage = (ImageView) view.findViewById
                    (R.id.f_image_view);
            viewHolder.fruitText = (TextView) view.findViewById(R.id.f_text_view);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.fruitImage.setImageResource(fruit.getId());
        viewHolder.fruitText.setText(fruit.getFruitname());
        return view;
    }


    class ViewHolder {
        ImageView fruitImage;
        TextView fruitText;
    }
}
